home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1993 / Internet Info CD-ROM (Walnut Creek) (1993).iso / networking / ip / ka9q / MacBMsrc.hqx / Mac bm Project / bmutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-15  |  16.5 KB  |  835 lines

  1. /* bmutil.c */
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <time.h>
  5. #include "global.h"
  6. #ifdef MAC
  7. #include <strings.h>
  8. #include <time.h>
  9. #include <types.h>
  10. #include <unix.h>
  11. #include "mac_stat.h"
  12. #else
  13. #include <string.h>
  14. #endif
  15. #ifndef MAC
  16. #define        SETVBUF
  17. #endif
  18. #if    defined(UNIX) || defined(MICROSOFT)
  19. #include    <sys/types.h>
  20. #endif
  21. #if    defined(UNIX) || defined(MICROSOFT) || defined(__TURBOC__)
  22. #include    <sys/stat.h>
  23. #endif
  24. #ifdef AZTEC
  25. #include <stat.h>
  26. #endif
  27. #ifndef MAC
  28. #include <fcntl.h>
  29. #endif
  30. #include "bm.h"
  31. #include "header.h"
  32.  
  33. char *getname();
  34. static unsigned long    mboxsize;
  35. static int    anyread;
  36.  
  37. #ifndef MAC
  38. #ifdef SETVBUF
  39. #define        MYBUF    4096
  40. char    *inbuf = NULLCHAR;    /* the stdio buffer for the mail file */
  41. char    *outbuf = NULLCHAR;    /* the stdio file io buffer for the temp file */
  42. #endif
  43. #endif
  44.  
  45. int
  46. initnotes()
  47. {
  48.     FILE    *tmpfile();
  49.     FILE    *ifile;
  50.     register struct    let *cmsg;
  51.     int     i, ret;
  52.     struct stat mstat;
  53.  
  54.     if (!stat(mfilename,&mstat))
  55.         mboxsize = mstat.st_size;
  56.     if ((ifile = fopen(mfilename,"r")) == NULLFILE) {
  57.         printf(nomail);
  58.         mfile = NULLFILE;
  59.         return 0;
  60.     } 
  61. #ifdef    SETVBUF
  62.     if (inbuf == NULLCHAR)
  63.         inbuf = malloc(MYBUF);
  64.     setvbuf(ifile, inbuf, _IOFBF, MYBUF);
  65. #endif
  66.     if ((mfile = tmpfile()) == NULLFILE) {
  67.         printf("Unable to create tmp file\n");
  68.         (void) fclose(ifile);
  69.         return -1;
  70.     }
  71. #ifdef SETVBUF
  72.     if (outbuf == NULLCHAR)
  73.         outbuf = malloc(MYBUF);
  74.     setvbuf(mfile, outbuf, _IOFBF, MYBUF);
  75. #endif
  76.     nmsgs = 0;
  77.     current = 0;
  78.     change = 0;
  79.     newmsgs = 0;
  80.     anyread = 0;
  81.     ret = readnotes(ifile);
  82.     (void) fclose(ifile);
  83.     if (ret != 0)
  84.         return -1;
  85. #ifdef SETVBUF
  86.     if (inbuf != NULLCHAR) {
  87.         (void) free(inbuf);
  88.         inbuf = NULLCHAR;
  89.     }
  90. #endif
  91.     for (cmsg = &mbox[1],i = 1; i <= nmsgs; i++, cmsg++)  
  92.         if ((cmsg->status & READ) == 0) {
  93.             newmsgs++;
  94.             if (current == 0)
  95.                 current = i;
  96.         }
  97.     /* start at one if no new messages */
  98.     if (current == 0)
  99.         current++;
  100.  
  101.     return 0;
  102.  
  103. }
  104.  
  105. /* readnotes assumes that ifile is pointing to the first
  106.  * message that needs to be read.  For initial reads of a
  107.  * notesfile, this will be the beginning of the file.  For
  108.  * rereads when new mail arrives, it will be the first new
  109.  * message.
  110.  */
  111. readnotes(ifile)
  112. FILE *ifile ;
  113. {
  114.     char     tstring[LINELEN];
  115.     long    cpos;
  116.     register struct    let *cmsg;
  117.     register char *line;
  118.     long    ftell();
  119.  
  120.     cmsg = (struct let *)NULL;
  121.     line = tstring;
  122.     while(!feof(ifile)) {
  123.         fgets(line,LINELEN,ifile);
  124.         /* scan for begining of a message */
  125.         if(strncmp(line,"From ",5) == 0) {
  126.             cpos = ftell(mfile);
  127.             fputs(line,mfile);
  128.             if (nmsgs == maxlet) {
  129.                 printf("Mail box full: > %d messages\n",maxlet);
  130.                 (void) fclose(mfile);
  131.                 return -1;
  132.             }
  133.             nmsgs++;
  134.             cmsg = &mbox[nmsgs];
  135.             cmsg->start = cpos;
  136.             cmsg->status = 0;
  137.             cmsg->size = strlen(line);
  138.             while (!feof(ifile)) {
  139.                 if (fgets(line,LINELEN,ifile) == NULLCHAR)
  140.                     break;
  141.                 if (*line == '\n') { /* done header part */
  142.                     cmsg->size++;
  143.                     putc(*line, mfile);
  144.                     break;
  145.                 }
  146.                 if (htype(line) == STATUS) {
  147.                     if (line[8] == 'R') 
  148.                         cmsg->status |= READ;
  149.                     continue;
  150.                 }
  151.                 cmsg->size += strlen(line);
  152.                 if (fputs(line,mfile) == EOF) {
  153.                     perror("tmp file");
  154.                     (void) fclose(mfile);
  155.                     return -1;
  156.                 }
  157.  
  158.             }
  159.         } else if (cmsg) {
  160.             cmsg->size += strlen(line);
  161.             fputs(line,mfile);
  162.         }
  163.     }
  164.     return 0;
  165. }
  166.  
  167. /* list headers of a notesfile a message */
  168. listnotes()
  169. {
  170.     register struct    let *cmsg;
  171.     register char    *p, *s;
  172.     char    smtp_date[SLINELEN];
  173.     char    smtp_from[SLINELEN];
  174.     char    smtp_subject[SLINELEN]; 
  175.     char    tstring[LINELEN];
  176.     int    i,c;
  177.     int    lines;
  178.     long    size;
  179.  
  180.     if (mfile == NULLFILE)
  181.         return;
  182.  
  183.     screen_clear();
  184.     setrawmode();
  185.     printf("Mailbox %s - %d messages %d new\n\n",mfilename,nmsgs,newmsgs);
  186.     lines = 2;
  187.     for (cmsg = &mbox[1],i = 1; i <= nmsgs; i++, cmsg++) {
  188.         *smtp_date = '\0';
  189.         *smtp_from = '\0';
  190.         *smtp_subject = '\0';
  191.         fseek(mfile,cmsg->start,0);
  192.         size = cmsg->size;
  193.         while (!feof(mfile) && size > 0) {
  194.             fgets(tstring,sizeof(tstring),mfile);
  195.             if (*tstring == '\n')    /* end of header */
  196.                 break;
  197.             size -= strlen(tstring);
  198.             rip(tstring);
  199.             /* handle continuation later */
  200.             if (*tstring == ' '|| *tstring == '\t')
  201.                 continue;
  202.             switch(htype(tstring)) {
  203.             case FROM:
  204.                 if((p = getname(tstring)) == NULLCHAR) {
  205.                     p = &tstring[6];
  206.                     while(*p && *p != ' ' && *p != '(')
  207.                         p++;
  208.                     *p = '\0';
  209.                     p = &tstring[6];
  210.                 }
  211.                 sprintf(smtp_from,"%.30s",p);
  212.                 break;
  213.             case SUBJECT:
  214.                 sprintf(smtp_subject,"%.34s",&tstring[9]);
  215.                 break;
  216.             case DATE:
  217.                 if ((p = strchr(tstring,',')) == NULLCHAR)
  218.                     p = &tstring[6];
  219.                 else
  220.                     p++;
  221.                 /* skip spaces */
  222.                 while (*p == ' ') p++;
  223.                 if(strlen(p) < 17)
  224.                     break;     /* not a valid length */
  225.                 s = smtp_date;
  226.                 /* copy day */
  227.                 if (atoi(p) < 10 && *p != '0') {
  228.                     *s++ = ' ';
  229.                 } else
  230.                     *s++ = *p++;
  231.                 *s++ = *p++;
  232.  
  233.                 *s++ = ' ';
  234.                 while (*p == ' ')
  235.                     p++;
  236.                 /* copy month */
  237.                 *s++ = *p++;
  238.                 *s++ = *p++;
  239.                 *s++ = *p++;
  240.                 while (*p == ' ')
  241.                     p++;
  242.                 /* skip year */
  243.                 while (isdigit(*p))
  244.                     p++;
  245.                 /* copy time */
  246.                 *s++ = *p++;    /* space */
  247.                 *s++ = *p++;    /* hour */
  248.                 *s++ = *p++;
  249.                 *s++ = *p++;    /* : */
  250.                 *s++ = *p++;    /* min */
  251.                 *s++ = *p++;
  252.                 *s = '\0';
  253.                 break;
  254.             case NOHEADER:
  255.                 break;
  256.             }
  257.         }
  258.         printf("%c%c%c%3d %-27.27s %-12.12s %5ld %.25s\n",
  259.             (i == current ? '>' : ' '),
  260.             (cmsg->status & DELETE ? 'D' : ' '),
  261.             (cmsg->status & READ ? 'Y' : 'N'),
  262.             i, smtp_from, smtp_date,
  263.             cmsg->size, smtp_subject);
  264.         if ((++lines % (MAXROWS - 1)) == 0) {
  265.             printf("- more -");
  266.             c = getrch();
  267.             printf("\r         \r");
  268.             if( c == EOF || c == 'q')
  269.                 break;
  270.             screen_clear();
  271.             lines = 0;
  272.         }
  273.     }
  274.     setcookedmode();
  275. }
  276.  
  277. /*  save msg on stream - if noheader set don't output the header */
  278. int
  279. msgtofile(msg,tfile,noheader)
  280. int msg;
  281. FILE *tfile;   /* already open for write */
  282. int noheader;
  283. {
  284.     char    tstring[LINELEN];
  285.     long     size;
  286.  
  287.     if (mfile == NULLFILE) {
  288.         printf(nomail);
  289.         return -1;
  290.     }
  291.     fseek(mfile,mbox[msg].start,0);
  292.     size = mbox[msg].size;
  293.     if ((mbox[msg].status & READ) == 0) {
  294.         mbox[msg].status |= READ;
  295.         change = 1;
  296.     }
  297.  
  298.     if (noheader) {
  299.         /* skip header */
  300.         while (!feof(mfile) && size > 0) {
  301.             fgets(tstring,sizeof(tstring),mfile);
  302.             size -= strlen(tstring);
  303.             if (*tstring == '\n')
  304.                 break;
  305.         }
  306.     }
  307.     while (!feof(mfile) && size > 0) {
  308.         fgets(tstring,sizeof(tstring),mfile);
  309.         size -= strlen(tstring);
  310.         fputs(tstring,tfile);
  311.         if (ferror(tfile)) {
  312.             printf("Error writing mail file\n");
  313.             (void) fclose(tfile);
  314.             return -1;
  315.         }
  316.     }
  317.     return 0;
  318. }
  319.  
  320. /*  delmsg - delete message in current notesfile */
  321. delmsg(msg)
  322. int msg;
  323. {
  324.     if (mfile == NULLFILE) {
  325.         printf(nomail);
  326.         return;
  327.     }
  328.     mbox[msg].status |= DELETE;
  329.     change = 1;
  330. }
  331.  
  332. /*  reply - to a message  */
  333. reply(msg)
  334. int msg;
  335. {
  336.     char     to[SLINELEN];
  337.     char    subject[LINELEN];
  338.     char    tstring[LINELEN];
  339.     char     *p,*s;
  340.     char    *toarg[1];
  341.     long    size;
  342.  
  343.     if (mfile == NULLFILE) {
  344.         printf(nomail);
  345.         return;
  346.     }
  347.     *to = '\0';
  348.     *subject = '\0';
  349.     fseek(mfile,mbox[msg].start,0);
  350.     size = mbox[msg].size;
  351.     while (!feof(mfile) && size > 0) {
  352.         fgets(tstring,sizeof(tstring),mfile);
  353.         size -= strlen(tstring);
  354.         if (*tstring == '\n')     /* end of header */
  355.             break;
  356.         rip(tstring);
  357.         if ((*to == '\0' && htype(tstring) == FROM)
  358.             || htype(tstring) == REPLYTO) {
  359.             s = getname(tstring);
  360.             if (s == NULLCHAR) {
  361.                 p = strchr(tstring,':');
  362.                 p += 2;
  363.                 s = p;
  364.                 while(*p && *p != ' ' && *p != '(')
  365.                     p++;
  366.                 *p = '\0';
  367.             }
  368.             *to = '\0';
  369.             strncat(to,s,sizeof(to));
  370.         } else if (htype(tstring) == SUBJECT) {
  371.             if (strncmp(&tstring[9], "Re:", 3))  /* No Re: yet? */
  372.                 sprintf(subject,"Re: %s\n",&tstring[9]);
  373.             else    /* there's an Re:, let's not add another */
  374.                 sprintf(subject,"%s\n",&tstring[9]) ;
  375.         }
  376.     }
  377.     if (*to == '\0')
  378.         printf("No reply address in message\n");
  379.     else {
  380.         toarg[0] = to;
  381.         dosmtpsend(NULLFILE,toarg,1,subject);
  382.     }
  383. }
  384.  
  385.  
  386. /* close the temp file while coping mail back to the mailbox */
  387. int
  388. closenotes()
  389. {
  390.     register struct    let *cmsg;
  391.     register char *line;
  392.     char tstring[LINELEN];
  393.     long size;
  394.     int i;
  395.     int ret;
  396.     FILE    *nfile;
  397.     struct stat mstat;
  398.  
  399.     if (mfile == NULLFILE)
  400.         return 0;
  401.     if (!change) {
  402.         (void) fclose(mfile);
  403.         return 0;
  404.     }
  405.     line = tstring;
  406.     fseek(mfile,0L,2);
  407.     if (isnewmail()) {
  408.         if ((nfile = fopen(mfilename,"r")) == NULLFILE)
  409.             perror(mfilename);
  410.         else {
  411.             /* seek to end of old msgs */
  412.             fseek(nfile,mboxsize,0);
  413.             /* seek to end of tempfile */
  414.             fseek(mfile,0L,2);
  415.             ret = readnotes(nfile);   /* get the new mail */
  416.             (void) fclose(nfile);
  417.             if (ret != 0) {
  418.                 printf("Error updating mail file\n");
  419.                 return -1;
  420.             }
  421.         }
  422.     }
  423.  
  424.     if ((nfile = fopen(mfilename,"w")) == NULLFILE) {
  425.         printf("Unable to open %s\n",mfilename);
  426.         return -1;
  427.     }
  428.     /* copy tmp file back to notes file */
  429.     for (cmsg = &mbox[1],i = 1; i <= nmsgs; i++, cmsg++) {
  430.         fseek(mfile,cmsg->start,0);
  431.         size = cmsg->size;
  432.         if ((cmsg->status & DELETE))
  433.             continue;
  434.         /* copy the header */
  435.         while (!feof(mfile) && size > 0) {
  436.             fgets(line,LINELEN,mfile);
  437.             size -= strlen(line);
  438.             if (*line == '\n') {
  439.                 if ((cmsg->status & READ) != 0)
  440.                     fprintf(nfile,"Status: R\n");
  441.                 fprintf(nfile,"\n");
  442.                 break;
  443.             }
  444.             fputs(line,nfile);
  445.         }
  446.         while (!feof(mfile) && size > 0) {
  447.             fgets(line,LINELEN,mfile);
  448.             fputs(line,nfile);
  449.             size -= strlen(line);
  450.             if (ferror(nfile)) {
  451.                 printf("Error writing mail file\n");
  452.                 (void) fclose(nfile);
  453.                 (void) fclose(mfile);
  454.                 return -1;
  455.             }
  456.         }
  457.     }
  458.     nmsgs = 0;
  459.     (void) fclose(nfile);
  460.     (void) fclose(mfile);
  461.     mfile = NULLFILE;
  462.  
  463.     /* remove a zero length file */
  464.     if (stat(mfilename,&mstat) == 0  && mstat.st_size == 0)
  465.         (void) unlink(mfilename);
  466.     return 0;
  467. }
  468.  
  469. /* get a message id from the sequence file */
  470. long 
  471. get_msgid()
  472. {
  473.     char sfilename[SLINELEN];
  474.     char s[20];
  475.     long sequence = 0L;
  476.     FILE *sfile;
  477.     long atol();
  478.  
  479. #ifdef MAC
  480.     sprintf(sfilename,"%ssequence.seq", mqueue);
  481. #else
  482.     sprintf(sfilename,"%s/sequence.seq", mqueue);
  483. #endif
  484.     sfile = fopen(sfilename,"r");
  485.  
  486.     /* if sequence file exists, get the value, otherwise set it */
  487.     if (sfile != NULLFILE) {
  488.         fgets(s,sizeof(s),sfile);
  489.         sequence = atol(s);
  490.     /* Keep it in range of and 8 digit number to use for dos name prefix. */
  491.         if (sequence < 0L || sequence > 99999999L )
  492.             sequence = 0L;
  493.         (void) fclose(sfile);
  494.     }
  495.  
  496.     /* increment sequence number, and write to sequence file */
  497.     sfile = fopen(sfilename,"w");
  498.     fprintf(sfile,"%ld",++sequence);
  499.     (void) fclose(sfile);
  500.     return sequence;
  501. }
  502.  
  503. /* Given a string of the form <user@host>, extract the part inside the
  504.  * brackets and return a pointer to it.
  505.  */
  506. char *
  507. getname(cp)
  508. register char *cp;
  509. {
  510.     char *cp1;
  511.  
  512.     if((cp = strchr(cp,'<')) == NULLCHAR)
  513.         return NULLCHAR;
  514.     cp++;    /* cp -> first char of name */
  515.     if((cp1 = strchr(cp,'>')) == NULLCHAR)
  516.         return NULLCHAR;
  517.     *cp1 = '\0';
  518.     return cp;
  519. }
  520.  
  521. /* create mail lockfile */
  522. int
  523. mlock(dir,id)
  524. char *dir;
  525. char *id;
  526. {
  527.     char lockname[SLINELEN];
  528.     int fd;
  529.     /* Try to create the lock file in an atomic operation */
  530. #ifdef MAC
  531.     sprintf(lockname,"%s%.8s.lck",dir,id);
  532. #else
  533.     sprintf(lockname,"%s/%.8s.lck",dir,id);
  534. #endif
  535.     if((fd = open(lockname, O_WRONLY|O_EXCL|O_CREAT,0600)) == -1)
  536.         return -1;
  537.     (void) close(fd);
  538.     return 0;
  539. }
  540.  
  541. /* remove mail lockfile */
  542. int
  543. rmlock(dir,id)
  544. char *dir;
  545. char *id;
  546. {
  547.     char lockname[SLINELEN];
  548. #ifdef MAC
  549.     sprintf(lockname,"%s%.8s.lck",dir,id);
  550. #else
  551.     sprintf(lockname,"%s/%.8s.lck",dir,id);
  552. #endif
  553.     (void) unlink(lockname);
  554. }
  555.  
  556. /* parse a line into argv array. Return argc */
  557. int
  558. parse(line,argv,maxargs)
  559. register char *line;
  560. char *argv[];
  561. int maxargs;
  562. {
  563.     int argc;
  564.     int qflag;
  565.     register char *cp;
  566.  
  567.     for(argc = 0; argc < maxargs; argc++)
  568.         argv[argc] = NULLCHAR;
  569.  
  570.     for(argc = 0; argc < maxargs;){
  571.         qflag = 0;
  572.         /* Skip leading white space */
  573.         while(*line == ' ' || *line == '\t')
  574.             line++;
  575.         if(*line == '\0')
  576.             break;
  577.         /* Check for quoted token */
  578.         if(*line == '"'){
  579.             line++;    /* Suppress quote */
  580.             qflag = 1;
  581.         }
  582.         argv[argc++] = line;    /* Beginning of token */
  583.         /* Find terminating delimiter */
  584.         if(qflag){
  585.             /* Find quote, it must be present */
  586.             if((line = strchr(line,'"')) == NULLCHAR){
  587.                 return -1;
  588.             }
  589.             *line++ = '\0';
  590.         } else {
  591.             /* Find space or tab. If not present,
  592.              * then we've already found the last
  593.              * token.
  594.              */
  595.             if((cp = strchr(line,' ')) == NULLCHAR
  596.                 && (cp = strchr(line,'\t')) == NULLCHAR){
  597.                 break;
  598.             }
  599.             *cp++ = '\0';
  600.             line = cp;
  601.         }
  602.     }
  603.     return argc;
  604. }
  605.  
  606. lockit()
  607. {
  608.     char line[SLINELEN];
  609.     while(mlock(maildir,notename)) {
  610.         printf("Mail file is busy, Abort or Retry ? ");
  611.         gets(line);
  612.         if (*line == 'A' || *line == 'a') {
  613.             if ( mfile != NULLFILE)
  614.                 (void) fclose(mfile);
  615.             mfile = NULLFILE;
  616.             return 1;
  617.         }
  618.     }
  619.     return 0;
  620. }
  621.  
  622. /* print the next message or the current on of new */
  623. printnext()
  624. {
  625.     if (mfile == NULLFILE)
  626.         return;
  627.     if ((mbox[current].status & READ) != 0) {
  628.         if (current == 1 && anyread == 0)
  629.             ;
  630.         else if (current < nmsgs) {
  631.             current++;
  632.         } else {
  633.             printf("Last message\n");
  634.             return;
  635.         }
  636.     }
  637.     displaymsg(current);
  638.     anyread = 1;
  639. }
  640.  
  641. /*  display message on the crt given msg number */
  642. displaymsg(msg)
  643. int msg;
  644. {
  645.     register int c;
  646.     register int col;
  647.     char    buf[MAXCOL+2];        /* line buffer */
  648.     int    lines;
  649.     int    cnt;
  650.     long     tsize, size;
  651.  
  652.     if (mfile == NULLFILE) {
  653.         printf(nomail);
  654.         return;
  655.     }
  656.     if( msg < 0 || msg > nmsgs) {
  657.         printf(badmsg,msg);
  658.         return;
  659.     }
  660.     setrawmode();
  661.     screen_clear();
  662.     fseek(mfile,mbox[msg].start,0);
  663.     size = mbox[msg].size;
  664.     tsize = size;
  665.  
  666.     printf("Message #%d %s\n",
  667.         msg, mbox[msg].status & DELETE ? "[Deleted]" : "");
  668.     if ((mbox[msg].status & READ) == 0) {
  669.         mbox[msg].status |= READ;
  670.         change = 1;
  671.     }
  672.     lines = 1;
  673.     col = 0;
  674.     while (!feof(mfile) && size > 0) {
  675.         for (col = 0;  col < MAXCOL-1;) {
  676.             c = getc(mfile);
  677.             size--;
  678.             if (feof(mfile) || size == 0)    /* end this line */
  679.                 break;
  680.             if (c == '\t') {
  681.                 cnt = col + 8 - (col & 7);
  682.                 if (cnt >= MAXCOL)    /* end this line */
  683.                     break;
  684.                 while (col < cnt)
  685.                     buf[col++] = ' ';
  686.             } else if (c == '\n')
  687.                 break;
  688.             else
  689.                 buf[col++] = c;
  690.         }
  691. #ifdef __TURBOC__
  692.         buf[col++] = '\n';
  693.         buf[col] = '\0';
  694.         cputs(buf);
  695. #else
  696.         buf[col] = '\0';
  697.         puts(buf);
  698. #endif
  699.         col = 0;
  700.         if ((++lines == (MAXROWS-1))) {
  701. #ifdef MAC
  702.             printf("- more -(%.f%%)",((float)(tsize-size)/tsize)*100.0);
  703. #else
  704.             printf("- more -(%d%%)",(tsize-size)*100/tsize);
  705. #endif
  706.             c = getrch();
  707.             printf("\r               \r");
  708.             if( c == EOF || c == 'q')
  709.                 break;
  710.             screen_clear();
  711.             lines = 0;
  712.         }
  713.     }
  714.     setcookedmode();
  715. }
  716.  
  717. /* list jobs wating to be sent in the mqueue */
  718. listqueue()
  719. {
  720.     char tstring[256];
  721.     char workfile[256];
  722.     char line[20];
  723.     char host[SLINELEN];
  724.     char to[SLINELEN];
  725.     char from[SLINELEN];
  726.     char *p;
  727.     char    status;
  728.     struct stat stbuf;
  729.     struct tm *tminfo, *localtime();
  730.     FILE *fp;
  731.  
  732.     printf("S     Job    Size Date  Time  Host                 From\n");
  733. #ifdef MAC
  734.     sprintf(workfile,"%s%s",mqueue,WORK);
  735. #else
  736.     sprintf(workfile,"%s/%s",mqueue,WORK);
  737. #endif
  738.     filedir(workfile,0,line);
  739.     while(line[0] != '\0') {
  740. #ifdef MAC
  741.         sprintf(tstring,"%s%s",mqueue,line);
  742. #else
  743.         sprintf(tstring,"%s/%s",mqueue,line);
  744. #endif
  745.         if ((fp = fopen(tstring,"r")) == NULLFILE) {
  746.             perror(tstring);
  747.             continue;
  748.         }
  749.         if ((p = strrchr(line,'.')) != NULLCHAR)
  750.             *p = '\0';
  751. #ifdef MAC
  752.         sprintf(tstring,"%s%s.lck",mqueue,line);
  753. #else
  754.         sprintf(tstring,"%s/%s.lck",mqueue,line);
  755. #endif
  756.         if (access(tstring,0))
  757.             status = ' ';
  758.         else
  759.             status = 'L';
  760. #ifdef MAC
  761.         sprintf(tstring,"%s%s.txt",mqueue,line);
  762. #else
  763.         sprintf(tstring,"%s/%s.txt",mqueue,line);
  764. #endif
  765.         stat(tstring,&stbuf);
  766. #ifdef MAC
  767.         tminfo = localtime(&stbuf.st_mtime);
  768. #else
  769.         tminfo = localtime(&stbuf.st_ctime);
  770. #endif
  771.         fgets(host,sizeof(host),fp);
  772.         rip(host);
  773.         fgets(from,sizeof(from),fp);
  774.         rip(from);
  775.         printf("%c %7s %7ld %02d/%02d %02d:%02d %-20s %s\n      ",
  776.             status, line, stbuf.st_size,
  777.             tminfo ->tm_mon+1,
  778.             tminfo->tm_mday,
  779.             tminfo->tm_hour,
  780.             tminfo->tm_min,
  781.             host,from);
  782.         while (fgets(to,sizeof(to),fp) != NULLCHAR) {
  783.             rip(to);
  784.             printf("%s ",to);
  785.         }
  786.         printf("\n");
  787.         (void) fclose(fp);
  788.         filedir(workfile,1,line);
  789.     }
  790. }
  791.  
  792. /* kill a job in the mqueue */
  793. killjob(j)
  794. char *j;
  795. {
  796.     char s[SLINELEN];
  797.     char tbuf[SLINELEN];
  798.     char *p;
  799. #ifdef MAC
  800.     sprintf(s,"%s%s.lck",mqueue,j);
  801. #else
  802.     sprintf(s,"%s/%s.lck",mqueue,j);
  803. #endif
  804.     p = strrchr(s,'.');
  805.     if (!access(s,0)) {
  806.         printf("Warning Job %s is locked by SMTP. Remove (y/n)? ",j);
  807.         gets(tbuf);
  808.         if (*tbuf != 'y')
  809.             return;
  810.         (void) unlink(s);
  811.     }
  812.     strcpy(p,".wrk");
  813.     if (unlink(s))
  814.         printf("Job id %s not found\n",j);
  815.     strcpy(p,".txt");
  816.     (void) unlink(s);
  817. }
  818.  
  819. /* check the current mailbox to see if new mail has arrived.
  820. * checks to see if the file has increased in size.
  821. * returns true if new mail has arrived.
  822. */
  823. isnewmail()
  824. {
  825.     struct stat mstat;
  826.     if (mfile == NULLFILE)
  827.         return 0;
  828.     if (stat(mfilename,&mstat))
  829.         printf("unable to stat %s\n",mfilename);
  830.     else if (mstat.st_size > mboxsize)
  831.             return 1;
  832.     return 0;
  833. }
  834.  
  835.